﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

public class WebClient
{
    public static HttpResponseMessage Post(string url, object obj, string mediaType = "application/json")
    {
        var uri = new Uri(url);
        HttpClient client = new HttpClient();
        StringContent content = null;
        if (obj.GetType() == typeof(string))
        {
            content = new StringContent((string)obj, Encoding.UTF8, mediaType);
        }
        else
        {
            content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, mediaType);
        }
        return client.PostAsync(url, content).Result;
    }

    public static string Request(string url, string querys, string bodys, string cert = null, string password = null, string method = "POST")
    {
        HttpWebRequest httpRequest = null;
        HttpWebResponse httpResponse = null;

        if (querys != null && 0 < querys.Length)
        {
            url = url + "?" + querys;
        }

        if (url.Contains("https://"))
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.KeepAlive = true;
            if (cert != null)
            {
                //InstallCertificate(cert, password, StoreLocation.CurrentUser, StoreName.My);
                httpRequest.ClientCertificates.Add(new X509Certificate2(cert, password));
                //X509Store store = new X509Store("My", StoreLocation.LocalMachine);
                //store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                //System.Security.Cryptography.X509Certificates.X509Certificate2 cert1 =
                //  store.Certificates.Find(X509FindType.FindBySubjectName, "昆明市盘龙区张先生电子商务经营部", false)[0];
                //httpRequest.ClientCertificates.Add(cert1);

            }
        }
        else
        {
            httpRequest = (HttpWebRequest)WebRequest.Create(url);
        }
        httpRequest.Method = method;
        if (bodys != null && 0 < bodys.Length)
        {
            byte[] data = Encoding.UTF8.GetBytes(bodys);
            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
        }
        httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        Stream st = httpResponse.GetResponseStream();
        StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
        return reader.ReadToEnd();
    }

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true;
    }

    public static HttpResponseMessage Get(string url)
    {
        var uri = new Uri(url);
        HttpClient client = new HttpClient();
        return client.GetAsync(url).Result;
    }
}
